<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class WorkDay extends CI_Controller {
 function __construct()
 {
   parent::__construct();
   $this->load->model('muser');
   $this->load->library('form_validation');
   $this->load->library('pagination');
   $this->load->helper('date');
   $this->load->library('my_time');
   $this->load->library('my_datatables');
 }
 
 function index(){
		
 	if ($this->input->post('Staff_ID')){
 		if ($this->session->userdata('StartDay'))
		 {
	 		$this->session->unset_userdata('StartDay');			
			$this->session->unset_userdata('EndDay');
			$this->session->unset_userdata('Staff_ID'); 
	 	}			
 		$dt=array(
	 		'StartDay'=>$this->input->post('FromDate'), 
	 		'EndDay'=>$this->input->post('ToDate'),
	 		'Staff_ID'=>$this->input->post('Staff_ID')
		 );
		 $this->session->set_userdata($dt);
		 $Staff_ID=$this->input->post('Staff_ID');
		 $this->nhap();		
 	}else{
 		$data['staff']	= $this->getStaffID();
 		$this->load->view('_header');
    	$this->load->view('workDay/StaffSelect_view',$data);
		//$this->load->view('workDay/workDay'); 
    	$this->load->view('_footer');
		
 	}
	
	
 }
   
 
 function nhap(){ 	 
	 
	 $Staff_ID=$this->session->userdata('Staff_ID');
	 if ($this->input->post('Date0')==NULL){
	 	$StartDay=$this->session->userdata('StartDay');
		 $EndDay=$this->session->userdata('EndDay');		
	 }else
	 {
	 	$Date0=$this->input->post('Date0');
		$kc=$this->input->post('kc');
	 	if ($this->input->post('next')){
	 		$StartDay = date("Y-m-d", strtotime($Date0)+$kc*86400);			
	 	}else{
	 		if ($Date0 > $this->session->userdata('StartDay') ){
	 			$StartDay=date("Y-m-d", strtotime($Date0)-7*86400);	
	 		}else{
	 			$StartDay=$Date0;				
	 		}
	 	}
		$EndDay=$this->session->userdata('EndDay');
		for ($i=0; $i <$kc ; $i++)
			 { 
				$data=array(
					'Staff_ID'=>$this->input->post('Staff_ID'),
					'Date'=>$this->input->post('Date'.$i),
					'StartTime'=>$this->input->post('StartTime'.$i),
					'EndTime'=>$this->input->post('EndTime'.$i),
					'WorkTime'=>$this->input->post('WorkTime'.$i),
					'OT'=>$this->input->post('OT'.$i),
					'ON'=>$this->input->post('ON'.$i),
					'WeekDay'=>$this->input->post('WeekDay'.$i),
					'DayType'=>$this->input->post('DayType'.$i)
				);
			if ($this->muser->checkWorkDay($data['Staff_ID'],$data['Date'])){
					$this->muser->addData('WorkDay',$data);							
				}else{
					$result=$this->muser->getWorkDay($data['Staff_ID'],$data['Date']);
					$WorkDay_ID=$result->WorkDay_ID;
					$this->muser->updateData('WorkDay','WorkDay_ID',$WorkDay_ID,$data);						
				}											
		}
				
	 }	 
	
	 if ($StartDay < $EndDay)
	 {
	 	$first=strtotime($StartDay);
		 $last=strtotime($EndDay);
		 $kc=abs($last - $first)/86400 + 1;
		 if ($kc > 7){
		 	$kc='7';
			}
				 	 
		$this->in($Staff_ID,$StartDay,$kc);
	 } else{
	 	
	 	$MonthTime_ID=$this->my_time->makeMonthTime($Staff_ID, $this->session->userdata['StartDay'], $EndDay);
		redirect('time/edit/'.$MonthTime_ID);
	 }		
		 

}


function in($Staff_ID,$start,$kc){
	$data['Staff_ID']=$Staff_ID;	
	$row=$this->muser->getRdata('Staff','Staff_ID',$Staff_ID);
	$data['name']=$row->FullName;
	$data['Date']=$start;
	$data['kc']=$kc;
	for ($i=0; $i <$kc ; $i++) { 
		if ($this->muser->getWorkDay($data['Staff_ID'],date("Y-m-d", strtotime($start)+$i*86400))!=NULL){
			$TM[$i]=$this->muser->getWorkDay($data['Staff_ID'],date("Y-m-d", strtotime($start)+$i*86400));
		}			
	}
	if (isset($TM )) $data['TM']=$TM;
	$this->load->view('_header');
    $this->load->view('workDay/workDay_view',$data); 
    $this->load->view('_footer');
	
}
function getStaffID(){
 	 $lab1=$this->muser->getData('Staff');
	 foreach ($lab1 as $row) {
		 $lab[$row->Staff_ID]=$row->Staff_ID.'-'.$row->FullName;
	 }
	 return $lab;
 }


}
 
  
